var str = location.href
var x = str.indexOf('=')
str = str.slice(x + 1)
console.log(str)
var bdlogin = getCookie('data')
var login = getCookie('login')
if (login) {
	$('#login').html(`<span>${login}</span>`)
	$('#regist').html(`<span>退出</span>`)
	$('#regist>span').click(function () {
		setCookie('login', 1, -180)
		location.reload()
	})
	var data = JSON.parse(localStorage.getItem('data1'))
	var x = data.reduce((pre, item) => {
		return pre + item.num * 1
	}, 0)
	$('#carr>a').html(`购物车(${x})`)
}
if (!login && bdlogin) {
	var data = JSON.parse(localStorage.getItem('data'))
	var x = data.reduce((pre, item) => {
		return pre + item.num * 1
	}, 0)
	$('#carr>a').html(`购物车(${x})`)
}
$.ajax({
	url: `/api/search?fcid=${str}`,
	type: 'get',
	success: function (res) {
		// res=JSON.parse(res)
		console.log(res)
		rander(res)
		lefth()
		fenye(res)
	},
})
function rander(res) {
	let str
	if (res.attributes) {
		str = res.attributes
			.map(function (item) {
				return `  <h2>${item.group}</h2>
        <div class="box">
            ${item.nameAndCounts
				.map(function (item2) {
					return `<span num='${item2.count}'>${item2.name}</span>`
				})
				.join('')}
        </div>`
			})
			.join('')
	} else {
		str = ''
	}

	let str2 = res.entities.data
		.map(function (item) {
			return `     <li>
                        <a href="./detalis.html?id=${
							item.id
						}" idnunber=${item.id}><img src="https:${item.mainImage}
      " alt=""></a>
        <a href="./detalis.html?id=${item.id}">
              <h2>【热卖】</h2>
              <h3>${item.name}</h3>
        </a>
        <p><span class="s1">￥ <b>${
			(item.price * 1) / 100
		}</b></span>    <span class="s2">已售${item.saleQuantity}台</span></p>
                    </li>`
		})
		.join('')
	$('.list .content').html(str2)
	$('.list .con').html(str)
	$('#num').html(`共${res.entities.total}条：`)
}
// 设置侧边栏样式
function lefth() {
	var yy = $('.list .con').outerHeight()
	// 记录一开始被撑开的高度后边要设置回原来的高度
	//  if(  $('.list .con').outerHeight()>$(window).height()){
	//      $('.list .con').css({
	//     height:$(window).height()
	// })
	// }
	// else{

	// }
	var flog = true
	var y = $('.list .con').offset().top
	// 变为固定定位时元素到window顶部的距离也会发生改变所以要先记下原来的位置

	var x = $('.list .right').outerHeight() - $(window).height() + y
	// x为滚动事件if判定条件
	console.log(x)
	$(window).scroll(function () {
		if (
			$(window).scrollTop() > y &&
			flog &&
			$(window).scrollTop() < y + x
		) {
			$('.list .con').css({
				top: 0,
				height: $(window).height(),
				position: 'fixed',
				overflow: 'auto',
			})
			flog = false
		}
		if ($(window).scrollTop() < y && !flog) {
			console.log(22)
			$('.list .con').css({
				position: 'relative',
				height: yy,
				overflow: 'visible',
			})
			flog = true
		}

		if ($(window).scrollTop() > x) {
			var xx = $(window).scrollTop() - x
			$('.list .con').css({
				top: -xx,
				overflow: 'auto',
			})
			flog = true
		}
	})
}
function fenye(res) {
	$('#m-style>div').pagination({
		pageCount: Math.ceil(res.entities.total / 20),
		// jump: true,
		coping: true,
		// isShow: true,
		prevContent: '上页',
		nextContent: '下页',
		// 点击的时候执行的函数
		callback: function (index) {
			console.log(index.getCurrent())
			$.ajax({
				url: `/api/search?fcid=${str}&pageNo=${index.getCurrent()}&pageSize=20`,
				type: 'get',
				success: function (res) {
					rander(res)
					// $(window).animate(scrollTop(0))
				},
			})
		},
	})
}
